import dayjs from 'dayjs'

/**
 * 根据时间周期获取开始和结束时间
 * @param {String} type 时间段类型
 * today:今天
 * yesterday:昨日
 * this_week:本周
 * last_week:上周
 * this_month:本月
 * last_month:上月
 * three_month:近三月
 * this_year:本年
 * last_year:去年
 * last_7_days:近7天
 * last_15_days:近15天
 * last_30_days:近30天
 * last_60_days:近60天
 * last_90_days:近90天
 * @returns
 */
export function getStartAndEndTime(type: string, format = 'YYYY-MM-DD') {
  let startDate = ''
  let endDate = dayjs().subtract(1, 'day').format(format)
  switch (type) {
    case 'yesterday':
      startDate = dayjs().subtract(1, 'day').format(format)
      break
    case 'this_week':
      startDate = dayjs().startOf('week').add(1, 'day').format(format)
      break
    case 'last_week':
      startDate = dayjs().add(-1, 'week').startOf('week').add(1, 'day').format(format)
      endDate = dayjs().add(-1, 'week').endOf('week').add(1, 'day').format(format)
      break
    case 'this_month':
      startDate = dayjs().startOf('month').format(format)
      break
    case 'last_month':
      startDate = dayjs().add(-1, 'month').startOf('month').format(format)
      endDate = dayjs().add(-1, 'month').endOf('month').format(format)
      break
    case 'three_month':
      startDate = dayjs().add(-2, 'month').startOf('month').format(format)
      break
    case 'this_year':
      startDate = dayjs().startOf('year').format(format)
      break
    case 'last_year':
      startDate = dayjs().add(-1, 'year').startOf('year').format(format)
      endDate = dayjs().add(-1, 'year').endOf('year').format(format)
      break
    case 'last_7_days':
      startDate = dayjs().subtract(7, 'day').format(format)
      break
    case 'last_15_days':
      startDate = dayjs().subtract(15, 'day').format(format)
      break
    case 'last_30_days':
      startDate = dayjs().subtract(30, 'day').format(format)
      break
    case 'last_60_days':
      startDate = dayjs().subtract(60, 'day').format(format)
      break
    case 'last_90_days':
      startDate = dayjs().subtract(90, 'day').format(format)
      break
    default:
      break
  }
  return [startDate, endDate]
}

// 时间快捷选项
export function rangeShortcuts(format = 'YYYY-MM-DD') {
  return [
    {
      label: '昨日',
      value: () => getStartAndEndTime('yesterday', format),
    },
    {
      label: '本月',
      value: () => getStartAndEndTime('this_month', format),
    },
    {
      label: '上月',
      value: () => getStartAndEndTime('last_month', format),
    },
    {
      label: '本年',
      value: () => getStartAndEndTime('this_year', format),
    },
    {
      label: '上年',
      value: () => getStartAndEndTime('last_year', format),
    },
    {
      label: '近7天',
      value: () => getStartAndEndTime('last_7_days', format),
    },
    {
      label: '近15天',
      value: () => getStartAndEndTime('last_15_days', format),
    },
    {
      label: '近30天',
      value: () => getStartAndEndTime('last_30_days', format),
    },
    {
      label: '近60天',
      value: () => getStartAndEndTime('last_60_days', format),
    },
    {
      label: '近90天',
      value: () => getStartAndEndTime('last_90_days', format),
    },
  ]
}
